const express = require('express')
const router = express.Router()
const PATH = '/demo2'
// const Joi = require('joi') // 验证参数
// 参考文档https://www.jianshu.com/p/879baf1cff07
/**
 * @swagger
 * /demo2/hello:
 *   post:
 *     tags:
 *       - 测试
 *     summary: Hello POST 测试
 *     parameters:
 *         - name: name
 *           description: Username to use for login.
 *           in: formData
 *           required: true
 *           type: string
 *     security:
 *          - Bearer: []
 *     description: 用于测试基础 GET 请求的接口
 *     responses:
 *       200:
 *         description: 【成功】 返回 world
 */
router.post('/hello', (req, res) => {
  res.send('demo2 world,' + req.body.name)
})

/**
   * @swagger
   * definitions:
   *   Login:
   *     required:
   *       - username
   *       - password
   *     properties:
   *       username:
   *         type: string
   *       password:
   *         type: string
   *       path:
   *         type: string
   */

/**
   * @swagger
   * tags:
   *   name: Users
   *   description: User management and login
   */

/**
   * @swagger
   * tags:
   *   - name: Login
   *     description: Login
   *   - name: Accounts
   *     description: Accounts
   */

/**
   * @swagger
   * /demo2/login:
   *   post:
   *     description: Login to the application
   *     tags: [Users, Login]
   *     produces:
   *       - application/json
   *     parameters:
   *       - $ref: '#/parameters/username'
   *       - name: password
   *         description: User's password.
   *         in: formData
   *         required: true
   *         type: string
   *     responses:
   *       200:
   *         description: login
   *         schema:
   *           type: object
   *           $ref: '#/definitions/Login'
   */
router.post('/login', (req, res) => {
  res.json(req.body)
})

/**
   * @swagger
   * /demo2/users:
   *   get:
   *     description: Returns users
   *     tags:
   *      - Users
   *     produces:
   *      - application/json
   *     responses:
   *       200:
   *         description: users
   */
router.get('/users', (req, res) => {
  res.json({
    username: 'jsmith'
  })
})

/**
   * @swagger
   * /demo2/users:
   *   post:
   *     description: Returns users
   *     tags: [Users]
   *     produces:
   *       - application/json
   *     parameters:
   *       - $ref: '#/parameters/username'
   *     responses:
   *       200:
   *         description: users
   */
router.post('/users', (req, res) => {
  res.json(req.body)
})

/**
 * @swagger
 * /demo2/all:
 *   put:
 *     summary: Creates a new user
 *     description:
 *       "Required roles: `admin`"
 *     tags:
 *       - Users
 *     parameters:
 *       - name: body
 *         in: body
 *         required: true
 *         schema:
 *           type: object
 *           required:
 *             - username
 *             - password
 *           properties:
 *             username:
 *               type: string
 *             password:
 *               type: password
 *           example: {
 *             "username": "someUser",
 *             "password": "somePassword"
 *           }
 *     responses:
 *       200:
 *         schema:
 *           type: object
 *           properties:
 *             id:
 *               type: integer
 *             username:
 *               type: string
 *         examples:
 *           application/json: {
 *             "id": 1,
 *             "username": "someuser"
 *           }
 *       409:
 *         description: When the username is already in use
 */
router.put('/all', (req, res) => {
  res.json(req.body)
})

module.exports = (app) => {
  app.use(PATH, router)
}
